/**
 * Created by levy on 16/6/15.
 */
/**
 * input type="text" 组件, 可以有默认值
 * 注意: defaultValue 只能设置一次
 * 参数:
 * addClass:
 * name:
 * value: 默认值
 * placeholder:
 *
 //组件交互函数,详见address-manage-edit.js
     blurFn
     focusFn
 */
var InputText = React.createClass({
    getDefaultProps(){
        return {
            focusFn: () => console.log('input focus'),
            blurFn: () => console.log('input blur'),
        }
    },
    getInitialState() {
        return {
            value: '',
            hasReceived: false,
        }
    },
    render() {
        var props = this.props,
            value = this.state.value,
            focusFn = props.focusFn,
            blurFn = props.blurFn;

        return (
            <input className={(props.addClass || '') + ' lf-input' } type="text"
                   name={props.name} id={props.name} value={value} placeholder={props.placeholder}
                   onChange={this._onChange}
                   onFocus={focusFn} onBlur={blurFn}/>
        )
    },
    componentWillReceiveProps(nextProps) {
        if(!nextProps.value || this.state.hasReceived)  return;

        this.setState({value: nextProps.value, hasReceived: true});
    },
    _onChange(e) {
        this.setState({value: e.target.value});
    }
});

export default InputText;
